<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%String path = request.getContextPath() ;%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %> 
<!DOCTYPE html>
<html lang="zh-CN" >
<head>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>组织架构映射区域</title>
<jsp:include page="../../import.jsp" flush="true" />
</head>

<body class="iframeBody">
<div class="Wrapper">
    <table class="layout2col1">
        <tbody>
            <tr>
                <td style="width: 280px;" valign="top">
                    <div style="width: 280px;" class="sqPanel">
                        <div class="sqPanel_head">
                            <span><i class="sqIco sqIco_3"></i>组织架构</span>
                        </div>
                        <div class="sqPanel_body" style="min-height: 450px;">
                            <div class="btnsBox">&nbsp;</div>
                            <div class="nanoWrap" style="height: 450px;">
                                <div class="nano"><div class="nano-content">
                                    <ul id="tree1">
                                    </ul>
                                </div></div>
                            </div>
                        </div>
                    </div>
                </td>
             <%--   <td style="width: 270px;" valign="top">
                    <div class="sqPanel">
                        <div class="sqPanel_head">
                            <form id="searchBar1" class="titSearchBar">
                                <div class="fieldset1 clearFix">
                                    <div class="fieldBox">
                                        <div class="label">角色</div>
                                        <div class="field">
                                             <input name="roleName" id="roleName" type="text" class="ui-textbox" value="">
                                        </div>
                                    </div>
                                    <span id="searchBtn1" class="sq-search-button"></i>查询</span>
                                </div>
                            </form>
                        </div>
                        <div class="sqPanel_body" style="min-height: 400px;">
                            <div class="FBL5">
                                <span id="addRole_btn" class="sq-button sq-button-hasicon"><i class="sqBtnIco_3"></i>新增</span>
                                <span id="deleteRole_btn" class="sq-button sq-button-hasicon"><i class="sqBtnIco_17"></i>移除</span>
                            </div>
                            <div id="ligergrid_roles" class="sq-grid" style="height: 450px;"> </div>
                        </div>
                    </div>
                </td>--%>
                <td width="300" valign="top">
                    <div class="sqPanel">
                        <div class="sqPanel_head">
                            <form id="searchBar1" class="titSearchBar">
                                <div class="fieldset1 clearFix">
                                    <div class="fieldBox">
                                        <div class="label">区域名称</div>
                                        <div class="field">
                                             <input name="regNm" id="regNm" type="text" class="ui-textbox" value="">
                                        </div>
                                    </div>
                                    <span id="searchBtn2" class="sq-search-button"></i>查询</span>
                                </div>
                            </form>
                        </div>
                        <div class="sqPanel_body" style="min-height: 300px;">
                            <div class="FBL5">
                                <span id="authoBtn" class="sq-button sq-button-hasicon"><i class="sqBtnIco_30"></i>授权</span>
                            </div>
                            <div class="nanoWrap" style="height: 320px;">
                                <div class="nano"><div class="nano-content">
                                    <ul id="tree2">
                                    </ul>
                                </div></div>
                            </div>
                        </div>
                    </div>
                </td>
            </tr>
        </tbody>
    </table>
</div>

<script type="text/javascript">

var treeOrg,
    treeReg,
    RESOURCE_IDs=[];
$(function () {
    treeOrg = $("#tree1").ligerTree({
    	url: '<%=path%>/system/sysOrg/initList.do',
        ajaxType: 'get',
        checkbox: false,
        // single: true,
        parentIcon: "SqFolder",
        childIcon: "SqLeaf",
        needCancel:false,
        isExpand: false,
        isLeaf: function (data) {
           /*  if (!data) return false;
            if (data.hasChildren) {return false} else {return true}; */
        	if (!data) return false;
            if (data.hasChildren) {return false} else if(data.children){return false} else {return true};
        },
        delay: function(e)
        {
           <%--  var data = e.data;
            if (data.hasChildren)
            {
                return { url: '<%=path%>/system/sysOrg/treeList.do?id=' + data.id }
            }
            return false; --%>
        },
        onAfterAppend: function (parentNode, newdata) {
            setNanoScroller();
        },
        onExpand: function (node) {
            setNanoScroller();
        },
        onCollapse: function (node) {
            setNanoScroller();
        },
        onSelect: function (node) {
            /*alert(JSON.stringify(node));*/
            searchRegionByOrgId(node.data.id);
            //set_treeReg(node.data.id);
            //resetResource(node.data.id);
        },
        onCancelselect: function (data,target) {
            debugger;
        },
        onSuccess: function (data) {
            setNanoScroller();
            treeOrg.selectNode(data[0]);
        }
    });
    treeReg = $("#tree2").ligerTree({
        url: '<%=path%>/system/sysOrgRegR/getTreeByOrgId.do',
        ajaxType: 'get',
        nodeWidth: 300,
        parentIcon: "SqFolder",
        childIcon: "SqLeaf",
        iconClsFieldName: "iconClass",
        enabledCompleteCheckbox: false,
        onCheck: function (item,checked) {
            if (!treeOrg.getSelected()) {
                top.$.ligerDialog.SQalert({content: LogUtils.LOG_SYS_ORG_SELECT_ORG,SQ_type: "warning"});
                treeReg.cancelSelect(item);
                return;
            };
            //alert("选择区域"+JSON.stringify(item.data.id));
            //var checkNodes=getCheckNodes(item.data);
            //setRESOURCE_IDs(checkNodes, checked);
            SQ.setIDs_onCheckTree(item, checked, treeReg, RESOURCE_IDs);
            if(RESOURCE_IDs.indexOf(item.data.id) >=0){
                RESOURCE_IDs.splice(RESOURCE_IDs.indexOf(item.data.id),1);
            }
        },
        onCancelSelect: function (data,target) {
            //alert("选择区域aaa")
            //debugger;
        },
        onSuccess: function (data) {
            setNanoScroller();
        }
    });
    $("#searchBar1").ligerForm();
    $("#searchBar1").click(function() {
        var form = liger.get("searchBar1");
        var data = form.getData();
        $.extend(true, data, { RESOURCE_IDs: RESOURCE_IDs});
        //alert(JSON.stringify(data));
        searchResource(data);
    });
    //查询区域资源BY ORGID
    function searchRegionByOrgId (id) {
        //alert(id);
        var parms={"orgId": id};
        $.ajax({
            url: '<%=path%>/system/sysOrgRegR/getRegionByOrgId.do',
            type: 'GET',
            dataType: 'json',
            data: parms,
            success: function (result) {
                //alert("searchRegionByOrgId=="+JSON.stringify(result));
                treeReg.setData(result.data.data);
                RESOURCE_IDs=result.data.IDs;
                //alert("searchRegionByOrgId =="+JSON.stringify(RESOURCE_IDs));
            }
        });
    }
    //查询区域资源
    function searchResource (parm) {
        $.ajax({
            url: '<%=path%>/system/sysOrgRegR/getByRegionNm.do',
            type: 'GET',
            dataType: 'json',
            data: parm,
            success: function (result) {
                treeReg.setData(result.data);
            }
        });
    }
    // 授权
    $("#authoBtn").click(function() {
        if (!treeOrg.getSelected()) {
            top.$.ligerDialog.SQalert({content: LogUtils.LOG_SYS_ORG_SELECT_ORG,SQ_type: "warning"});
            return;
        };
        if (!RESOURCE_IDs) {
            top.$.ligerDialog.SQalert({content: "请选择区域信息！",SQ_type: "warning"});
            return;
        };

        regionIdArr=RESOURCE_IDs;
        //alert("regionIdArr === "+regionIdArr);
        //tree.getSelected().data.id
        //getSelectedRow().id
        orgId =treeOrg.getSelected().data.id;
        //alert("orgId === "+orgId);
        $.ajax({
            url: '<%=path%>/system/sysOrgRegR/addRegToOrg.do',
            type: 'post',
            dataType: 'json',
            data:{'orgId':orgId,'regionIdArr[]':regionIdArr},
            success:function(msg){
                if(msg.success == 1){
                    top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "success"});
                }else{
                    top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
                }
            }
        });
        //ligerDialogSuc("授权成功！",1000)
    });
});

/*function set_treeReg (id) {
    var parms={"orgId": id};
    if (grid_roles) {
        grid_roles.grid.show();
        grid_roles.setParm("orgId", id);
        grid_roles.reload();
    }else{
        render_ligergrid_roles (parms)
    };
}*/
function setRESOURCE_IDs (checkNodes, ischecked) {
    if (ischecked) {
        for (var i = 0; i < checkNodes.length; i++) {
            var checkedID=checkNodes[i].id;
            if(RESOURCE_IDs.indexOf(checkedID)==-1) RESOURCE_IDs.push(checkedID);
        };
    } else{
        for (var i = 0; i < checkNodes.length; i++) {
            var checkedID=checkNodes[i].id;
            var index=RESOURCE_IDs.indexOf(checkedID);
            if(index>=0) RESOURCE_IDs.splice(index,1);
        };
    };
}
// 获取tree check或uncheck的时候,触发的所有node，包括子节点
function getCheckNodes (data) {
    var checkNodes=[];
    function findNodes (node) {
        checkNodes.push(node);
        if (treeReg.hasChildren(node)) {
            for (var i = 0; i < node.children.length; i++) {
                var childrenNode=node.children[i];
                findNodes (childrenNode);
            };
        }else{
            return;
        };
    }
    findNodes (data);
    return checkNodes;
}

function resetResource (orgId) {
    var parms={"orgId": orgId};
    $.ajax({
        url: '<%=path%>/system/sysOrgRegR/getRegionByOrgId.do',
        async: false,
        type: 'GET',
        dataType: 'json',
        data: parms,
        success: function (result) {
            //alert("searchRegionByOrgId=="+JSON.stringify(result));
            //treeReg.setData(result.data.data);
            RESOURCE_IDs=result.data.IDs;
            //alert("searchRegionByOrgId=="+JSON.stringify(RESOURCE_IDs));
        }
    });
    /*$.ajax({
        url: '<%=path%>/system/sysRoleResR/getByRoleId.do?roleId='+roleId,
        type: 'GET',
        dataType: 'json',
        success: function (result) {
            tree.setData(result.data.data);
            RESOURCE_IDs=result.data.IDs;
            //console.log(RESOURCE_IDs);
            //alert(RESOURCE_IDs);
        }
    });*/
}

function setNanoScroller () {
    $(".nanoWrap .nano").nanoScroller({
        preventPageScrolling: true
    });
}
</script>

</body>
</html>
